package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.dal.base.enums.ExpressOrderType;
import com.zbkj.dal.base.enums.OrderAddressBizType;
import com.zbkj.dal.base.enums.OrderAddressType;
import com.zbkj.dal.dao.OrderAddressMapper;
import com.zbkj.dal.entity.order.OrderAddress;
import com.zbkj.service.model.dto.address.OrderAddressCreateDTO;
import com.zbkj.service.model.dto.express.OrderAddressUpdateDTO;
import com.zbkj.service.service.OrderAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderAddressServiceImpl extends ServiceImpl<OrderAddressMapper, OrderAddress> implements OrderAddressService {

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Override
    public List<OrderAddress> getOrderAddressList(String orderNo, String subBizType) {
        LambdaQueryWrapper<OrderAddress> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderAddress::getOrderNo, orderNo);
        lqw.eq(OrderAddress::getBusinessType, subBizType);
        return orderAddressMapper.selectList(lqw);
    }

    @Override
    public OrderAddress getOneOrderAddressList(String orderNo, OrderAddressType addressType, ExpressOrderType subBizType) {
        LambdaQueryWrapper<OrderAddress> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderAddress::getOrderNo, orderNo);
        lqw.eq(OrderAddress::getAddressType, addressType.getType());
        lqw.eq(OrderAddress::getBusinessType, subBizType.name());
        return orderAddressMapper.selectOne(lqw);
    }

    @Override
    public Boolean insertAddress(String orderNo, OrderAddressType addressType, ExpressOrderType orderSubStageType, OrderAddressCreateDTO createDTO) {
        if (createDTO == null) {
            return false;
        }

        // 如果 订单 & 地址类型 & 订单子阶段类型 存在，则跳过insert
        if (this.getOneOrderAddressList(orderNo, addressType, orderSubStageType) != null) {
            return true;
        }

        OrderAddress orderAddress = new OrderAddress();
        orderAddress.setOrderNo(orderNo);
        orderAddress.setAddressType(addressType.getType());
        orderAddress.setBusinessType(orderSubStageType.name());
        orderAddress.setLinkAddressId(createDTO.getLinkAddressId());
        orderAddress.setProvinceCode(createDTO.getProvinceCode());
        orderAddress.setProvinceName(createDTO.getProvinceName());
        orderAddress.setCityCode(createDTO.getCityCode());
        orderAddress.setCityName(createDTO.getCityName());
        orderAddress.setRegionCode(createDTO.getRegionCode());
        orderAddress.setRegionName(createDTO.getRegionName());
        orderAddress.setAddress(createDTO.getAddress());
        orderAddress.setContactName(createDTO.getContactName());
        orderAddress.setContactPhone(createDTO.getContactPhone());
        orderAddress.setCreateTime(LocalDateTime.now());
        orderAddress.setUpdateTime(LocalDateTime.now());
        return this.save(orderAddress);
    }

    @Override
    public Boolean updateAddressByAddressId(OrderAddressUpdateDTO orderAddressUpdate) {
        if (orderAddressUpdate == null) {
            return false;
        }
        OrderAddress orderAddress = super.getById(orderAddressUpdate.getId());
        orderAddress.setProvinceCode(orderAddressUpdate.getProvinceId());
        orderAddress.setProvinceName(orderAddressUpdate.getProvince());
        orderAddress.setCityCode(orderAddressUpdate.getCityId());
        orderAddress.setCityName(orderAddressUpdate.getCity());
        orderAddress.setRegionCode(orderAddressUpdate.getDistrictId());
        orderAddress.setRegionName(orderAddressUpdate.getDetail());
        orderAddress.setAddress(orderAddressUpdate.getDetail());
        orderAddress.setContactName(orderAddressUpdate.getRealName());
        orderAddress.setContactPhone(orderAddressUpdate.getPhone());
        orderAddress.setUpdateTime(LocalDateTime.now());
        return this.saveOrUpdate(orderAddress);
    }
}
